package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.BenefitSettingBO;
import com.junzhiit.nengji.cms.service.BenefitSettingService;
import com.junzhiit.nengji.cms.web.exception.BenefitSettingNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.BenefitSettingVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.BenefitSettingDAO;
import com.junzhiit.nengji.model.BenefitSetting;

@Service
public class BenefitSettingServiceImpl extends BaseServiceImpl implements BenefitSettingService{
	
	private static final Double MIN=5.00;
	
	private static final Double MAX=100.00;
	
	@Autowired
	private BenefitSettingDAO benefitSettingDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, BenefitSettingVO benefitSettingVO) {
		BenefitSetting benefitSetting=new BenefitSetting();
		BeanUtils.copyProperties(benefitSettingVO, benefitSetting);
		
		List<BenefitSetting> list=benefitSettingDAO.findAll(benefitSetting,  dtp.getStart(), dtp.getLength());
		int totalCount = benefitSettingDAO.getTotalCount(benefitSetting);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		BenefitSetting benefitSetting=benefitSettingDAO.query(id);
		if(benefitSetting == null){
			throw new BenefitSettingNotFoundException("让利类别不存在");
		}
		BenefitSetting nextBenefitSetting = benefitSettingDAO.findNextByBenefit(id);
		BenefitSetting prevBenefitSetting = benefitSettingDAO.findPrevByBenefit(id);
		BenefitSettingBO benefitSettingBO = new BenefitSettingBO();
		BeanUtils.copyProperties(benefitSetting, benefitSettingBO);
		if(prevBenefitSetting != null){
			benefitSettingBO.setPrevStartBenefit(prevBenefitSetting.getStartBenefit());
		}else{
			benefitSettingBO.setPrevStartBenefit(MIN-1);
		}
		if(nextBenefitSetting != null){
			benefitSettingBO.setNextEndBenefit(nextBenefitSetting.getEndBenefit());
		}else{
			benefitSettingBO.setNextEndBenefit(MAX+1);
		}
		
		result.setData(benefitSettingBO);
		return result;
	}

	@Override
	public ResultVO add(BenefitSettingVO benefitSettingVO) {
		ResultVO result = new ResultVO();
		BenefitSetting benefitSetting=new BenefitSetting();
		BeanUtils.copyProperties(benefitSettingVO, benefitSetting);
		benefitSettingDAO.insert(benefitSetting);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		BenefitSetting benefitSetting = benefitSettingDAO.query(id);
		if(benefitSetting == null){
			throw new BenefitSettingNotFoundException("让利类别不存在");
		}
		BenefitSetting nextBenefitSetting = benefitSettingDAO.findNextByBenefit(id);
		if(nextBenefitSetting != null){
			nextBenefitSetting.setStartBenefit(benefitSetting.getStartBenefit());
			benefitSettingDAO.update(nextBenefitSetting);
		}
		benefitSettingDAO.updateToDelete(id);
		return result;
	}

	@Override
	public ResultVO update(BenefitSettingVO benefitSettingVO) {
		ResultVO result = new ResultVO();
		BenefitSetting benefitSetting=benefitSettingDAO.query(benefitSettingVO.getId());
		if(benefitSetting == null){
			throw new BenefitSettingNotFoundException("让利类别不存在");
		}
		BenefitSetting nextBenefitSetting = benefitSettingDAO.findNextByBenefit(benefitSettingVO.getId());
		BenefitSetting prevBenefitSetting = benefitSettingDAO.findPrevByBenefit(benefitSettingVO.getId());
		BeanUtils.copyProperties(benefitSettingVO, benefitSetting);
		
		if(prevBenefitSetting != null){
			prevBenefitSetting.setEndBenefit(benefitSetting.getStartBenefit());
			benefitSettingDAO.update(prevBenefitSetting);
		}
		
		if(nextBenefitSetting != null){
			nextBenefitSetting.setStartBenefit(benefitSetting.getEndBenefit());;
			benefitSettingDAO.update(nextBenefitSetting);
		}
		benefitSettingDAO.update(benefitSetting);
		return result;
	}

	@Override
	public ResultVO getBenefitMax() {
		ResultVO result = new ResultVO();
		Double max=benefitSettingDAO.getBenefitMax();
		if(max < MIN){
			max = MIN;
		}
		BenefitSetting benefitSetting=new BenefitSetting();
		benefitSetting.setStartBenefit(max);
		benefitSetting.setEndBenefit(MAX);
		result.setData(benefitSetting);
		return result;
	}
}
